package com.esun.payment_api.service.impl;


import com.esun.common.utils.PinyinUtils;
import com.esun.payment_api.domain.WldwList;
import com.esun.payment_api.dto.WldwQueryParam;
import com.esun.payment_api.mapper.WldwListMapper;
import com.esun.payment_api.service.WldwListService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WldwListServiceImpl implements WldwListService{

    @Resource
    private WldwListMapper wldwListMapper;


    @Override
    public List<WldwList> getList(WldwQueryParam wldwQueryParam, Integer co_id, Integer deptId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<WldwList> list = wldwListMapper.getList(wldwQueryParam, co_id, deptId);
        if (list != null && list.size() > 0) {
            //Comparator com = Collator.getInstance(Locale.CHINA);
            //按字母排序
            // Collections.sort(list.stream().map(WldwList::getName).collect(Collectors.toList()), com);

            //循环找出首字母一样的数据
            for (WldwList e : list) {
                try {
                    String name = e.getName();
                    name = name.replaceAll("《", "<");
                    name = name.replaceAll("》", ">");
                    name = name.replaceAll("（", "(");
                    name = name.replaceAll("）", ")");
                    String zm = PinyinUtils.getFullSpell(name).substring(0, 1);
                    Character character = zm.charAt(0);
                    System.out.println(character);
                    if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
                        Character firstCharacter = name.substring(0, 1).charAt(0);
                        if ((firstCharacter >= 'a' && firstCharacter <= 'z') || (firstCharacter >= 'A' && firstCharacter <= 'Z')) {
                            e.setInitials("#");
                        } else {
                            e.setInitials(zm);
                        }
                    } else {
                        e.setInitials("#");
                    }
                } catch (Exception exception) {
                    e.setInitials("#");
                }
            }

//            Collections.sort(list, new Comparator<WldwList>() {
//                @Override
//                public int compare(WldwList o1, WldwList o2) {
//                    return o1.getInitials().charAt(0) - o2.getInitials().charAt(0);
//                }
//            });
        }
        return list;
    }
}
